<?php
namespace app\admin\controller;
use think\AjaxPage;
use think\Page;
use think\Db;
use think\Loader;

class Custom extends Base {
	public function index(){
		$count =  M('custom')->count();
    	$Page = new Page($count,10);
        $show = $Page->show();
        $lists = M('custom')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('lists',$lists);
        $this->assign('pager',$Page);// 赋值分页输出
        $this->assign('page',$show);// 赋值分页输出   
		return $this->fetch();
	}
	
	//编辑模板信息
	public function editcustom(){

       $cid = I('id');
        
       if (IS_POST) {
       	$data = I('post.');
		   
		$cvalidate = Loader::validate('Custom');
        
		
		 		 
		
		//exit(json_encode(['att'=>$att,'value'=>$attvalue,'atype'=>$atype]));
		
       	if ($cid > 0) {
       		//修改操作
       		$cvalidate->setRule([
		        'id'=>'checkId',
		        'name'=>'require|max:50'
		    ]);
			
			if (!$cvalidate->batch()->check($data)) {
	            $return = ['status' => 0, 'msg' => '操作失败', 'result' => $cvalidate->getError()];
	            $this->ajaxReturn($return);
        	}
			      		
       		M('custom')->where(['id'=>$cid])->save($data);
       	}
		else{
			//添加操作
			$cvalidate->setRule([
		        'id'=>'checkId',
		        'name'=>'require|unique:custom|max:50'
		    ]);
			
			unset($data['id']);
			$data['addtime'] = time();
			if (!$cvalidate->batch()->check($data)) {
	            $return = ['status' => 0, 'msg' => '操作失败', 'result' => $cvalidate->getError()];
	            $this->ajaxReturn($return);
        	}
			$cid = M('custom')->add($data);
		}
		
		
		//属性值添加
		if($cid>0){
			$att = I('att/a',[]);
			$attvalue = I('attvalue/a',[]);
			$atype = I('atttype/a',[]);
			$attlen = count($att);
			//如果数据长度不对
			if(count($attvalue)==$attlen && count($atype)==$attlen){
				M("custom_att")->where(['customid'=>$cid])->delete();
				
				
				for($i = 0;$i<$attlen;$i++){
					M("custom_att")
					->add(['customid'=>$cid,'attname'=>$att[$i],'attvalue'=>$attvalue[$i],'atttype'=>$atype[$i]]);
				}
			}
		}
		
		$this->ajaxReturn(['status' => 1, 'msg' => '操作成功']);
	   }
       $custom_list = M('custom')->where(['id'=>$cid])->find();
       $this->assign('info', $custom_list);
	   $atts = M('custom_att')->where('customid',$cid)->select();
	   $this->assign('custom_att', $atts);
	   return $this->fetch();
	}


	public function custom_del()
    {
        $cid = I('id/d',0);
        $c_order = M('user_custom')->where("coutomid = $cid")->find();
        if (!empty($c_order)) {
            $this->ajaxReturn(['status' => -1, 'msg' => '已经用用户使用该模块']);
        }
		
        M('custom')->where("id=$cid")->delete();
		M('custom_att')->where("customid=$cid")->delete();
        $this->ajaxReturn(['status' => '1', 'msg' => '操作成功'.$cid]);
    }
	
	//用户定制信息列表
	public function userindex(){
        return $this->fetch();
	}
	
	public function userindexajax(){
		$where = '1 = 1'; // 搜索条件                

        // 关键词搜索               
        $key_word = I('key_word') ? trim(I('key_word')) : '';
        if($key_word)
        {
            $where = "$where and (uname like '%$key_word%')" ;
        }
        
        $count = M('user_custom')->where($where)->count();
        $Page  = new AjaxPage($count,20);
		$orderby1 = I('orderby1');
		$orderby2 = I('orderby2');
		
        $show = $Page->show();
        $order_str = "$orderby1 $orderby2";
		$startpage = $Page->firstRow;
		$endpage = $Page->listRows;
		
		$uList = Db::view('tp_user_custom','*')
	    ->view('tp_custom','id,name,message','tp_user_custom.coutomid=tp_custom.id')
	    ->where($where)
		->order($order_str)->limit($startpage.','.$endpage)
	    ->select();
		
//      $uList = Db::query("select tp_user_custom.* from tp_custom,tp_user_custom where tp_custom.id = tp_user_custom.coutomid and $where $order_str limit $startpage,$endpage");//M('user_custom')->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();
		
//		exit(json_encode($uList));
		$statemsg = C('CUSTOMSTATE');
		$this->assign('cstate', $statemsg);
        $this->assign('lists',$uList);
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch();
	}
	
	
	//编辑用户定制信息
	public function editusercustom(){
		
		$ids = input('tuid/d',0);
		
		if(IS_POST){
			if($ids>0){
				$data = I('post.');
				
				$cvalidate = Loader::validate('editusercustom');
				if (!$cvalidate->batch()->check($data)) {
		            $return = ['status' => 0, 'msg' => '操作失败', 'result' => $cvalidate->getError()];
		            $this->ajaxReturn($return);
	        	}

				
				$data['confirmtime'] = time();
				M("user_custom")->where("tuid = $ids")->save($data);
		        $this->ajaxReturn(['status' => '1', 'msg' => '操作成功']);
			}
		}
		
		$info = M("user_custom")->where("tuid = $ids")->find();
		if(empty($info)){
			$this->assign('info', []);
			$this->assign('lists', []);
		}
		else{
			$lists = M("user_custom_att")->where(['cid'=>$ids])->select();
			
			foreach($lists as $k=>$v){
				if($v['tp'] == 2){
					$lists[$k]['imgarr'] = explode(',',$v['attvalue']);
				}
			}
			
			$this->assign('info', $info);
			$this->assign('lists', $lists);
			
		}
		$statemsg = C('CUSTOMSTATE');
		$this->assign('cstate', $statemsg);
		return $this->fetch();
	}
}
?>